function [w, x, y, status] = roms_depaveuv ( rslice_data, timestep )
% ROMS_depaveuv:
%
% USAGE:  [w, x, y, status] = roms_depaveuv ( rslice_data, timestep);
%
% PARAMETERS:
% Input:
%     rslice_data:  global data structure
%     timestep:  zero-based time index
% Output:
%     w:  complex velocity vector w=(u + i*v) rotated by roms angle
%     x, y:  defines the grid on which w is defined
%     status:  -1 if the routine fails
%
% USES:  roms_zslice
%
% AUTHOR:  John Evans (jevans@marine.rutgers.edu)
%

w = [];
x = [];
y = [];
status = -1;

%
% Get the u and v.  Must name them specifically.

ncfile= rslice_data.ncfile_list{rslice_data.ncfile_index};

u = nc_varget(ncfile,'ubar',[timestep 0 0],[1 -1 -1]);
v = nc_varget(ncfile,'vbar',[timestep 0 0],[1 -1 -1]);


x=rslice_data.rslice_grid.x.data(2:end-1,2:end-1);
y=rslice_data.rslice_grid.y.data(2:end-1,2:end-1);

u = (u(2:end-1,1:end-1) + u(2:end-1,2:end))/2;                                                                     
v = (v(1:end-1,2:end-1) + v(2:end,2:end-1))/2;
w = u+i*v;

%
% Now apply the rotation
switch ( rslice_data.rslice_grid.coordsys )
case 'xy'
	;
case 'geographic'
	angle = rslice_data.rslice_grid.angle.data(2:end-1,2:end-1);
	w=w.*exp(sqrt(-1)*angle);
	

otherwise
	msg = sprintf ( '%s:  unhandled coordsys ''%s''\n', mfilename, rslice_data.rslice_grid.coordsys );
	rslice_error ( msg );
end
if isfield(rslice_data.rslice_grid.roms_grid,'mask_rho_nan');
   w=w.*rslice_data.rslice_grid.roms_grid.mask_rho_nan(2:end-1,2:end-1);
end

return
